public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode CreatTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');

        A.left = B;
        A.right = C;
        B.left = D;
        C.left = E;
        C.right = F;

        return A;
    }

    public int CountSize = 0;

    public void NodeSize(TreeNode root){
        if(root == null){
            return;
        }
        CountSize++;
        NodeSize(root.left);
        NodeSize(root.right);
    }

    public int NodeSize2(TreeNode root){
        if(root == null){
            return 0;
        }
        int tmp = NodeSize2(root.left)+NodeSize2(root.right)+1;
        return tmp;
    }

    //获取叶子结点个数
    public int getLeafNodeCount(TreeNode root){
        if(root == null){
            return 0;
        }else if(root.left == null && root.right == null){
            return 1;
        }else{
            return getLeafNodeCount(root.left)
                    + getLeafNodeCount(root.right);
        }
    }

    public int LeafCount;

    //遍历问题
    public void getLeafNodeCount2(TreeNode root){
        if(root == null){
            return;
        }
        if(root.left == null && root.right == null){
            LeafCount++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }

    //获取第k层结点的个数
    public int getLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
        return getLevelNodeCount(root.right,k-1) +
                getLevelNodeCount(root.left,k-1);
    }

    //获取二叉树的高度
    public int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return Math.max(leftH,rightH) + 1;
    }

    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root,int val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }

        TreeNode leftVal = find(root.left,val);
        if(leftVal != null){
            return leftVal;
        }

        TreeNode rightVal = find(root.right,val);
        if(rightVal != null){
            return rightVal;
        }

        return null;
    }
}
